package com.wlm.controller;

import com.wlm.Utils.ResultUtil;
import com.wlm.Utils.TimeUtitl;
import com.wlm.dto.rep.UserQueryReqDto;
import com.wlm.dto.resp.PageRespDto;
import com.wlm.entity.PgUser;
import com.wlm.entity.model.ResultFormat;
import com.wlm.service.PgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-05-19
 */
@RestController
@RequestMapping("/pgUser/")
public class PgUserController {

    @Autowired
    private PgUserService pgUserService;


    /**
     * 查询用户列表
     *
     * @return
     */
    @PostMapping("queryUserList")
    public ResultFormat queryUserList(UserQueryReqDto userQueryReqDto) {
        PageRespDto<PgUser> pgUsersIPage = pgUserService.queryUserList(userQueryReqDto);
        return ResultUtil.success(pgUsersIPage);
    }

    /**
     * 分页查询用户列表
     *
     * @return
     */
    @PostMapping("queryUserList2")
    public ResultFormat queryUsersByPage(@RequestBody UserQueryReqDto userQueryReqDto) {
        PageRespDto<PgUser> pgUsersIPage = pgUserService.queryUserList(userQueryReqDto);
        return ResultUtil.success(pgUsersIPage);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("delUser/{id}")
    public ResultFormat delUser(@PathVariable  String id) {
        System.out.println(id);
        boolean removeById = pgUserService.removeById(id);
        System.out.println(removeById);
        if (removeById) {
            return ResultUtil.success(200, "删除成功");
        }
        return ResultUtil.error(100, "删除失败");

    }

    /**
     * 添加用户
     *
     * @param pgUser
     * @return
     */
    @PostMapping("saveUser")
    public ResultFormat saveUser(@RequestBody PgUser pgUser) {
        System.out.println(pgUser);
        pgUser.setUcreatetime(TimeUtitl.dateTime());
        boolean save = pgUserService.save(pgUser);
        System.out.println(save);
        if (save) {
            return ResultUtil.success(200, "添加成功");
        }
        return ResultUtil.error(100, "添加失败");
    }

    /**
     * 修改用户
     *
     * @param pgUser
     * @return
     */
    @PutMapping("editUser")
    public ResultFormat editUser(@RequestBody PgUser pgUser) {
        System.out.println(pgUser);
        boolean updateById = pgUserService.updateById(pgUser);
        System.out.println(updateById);
        if (updateById) {
            return ResultUtil.success(200, "修改成功");
        }
        return ResultUtil.error(100, "修改失败");
    }

}
